package com.wzd.testimage.view;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by hello on 15-5-28.
 */
public class MyImageViewMatirx extends ImageView {
    public MyImageViewMatirx(Context context) {
        super(context);
        setScaleType(ScaleType.MATRIX);
    }

    public MyImageViewMatirx(Context context, AttributeSet attrs) {
        super(context, attrs);
        setScaleType(ScaleType.MATRIX);
    }

    public MyImageViewMatirx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setScaleType(ScaleType.MATRIX);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            rotateImage(event);
            scaleImage(event);

        }
        return true;
    }

    private void rotateImage(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                oldRotation = rotation(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mMatrixScale.postRotate(rotation(event) - oldRotation, getWidth() / 2, getHeight() / 2);
                this.setImageMatrix(mMatrixScale);
                oldRotation = rotation(event);
                break;
        }
    }

    // 取旋转角度
    private float rotation(MotionEvent event) {
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }

    private Matrix mMatrixScale = new Matrix();
    private float downLen;
    private float oldRotation;

    private void scaleImage(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                float downX = event.getX(1) - event.getX(0);
                float downy = event.getY(1) - event.getY(0);
                downLen = (float) Math.sqrt(downX * downX + downy * downy);
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX(1) - event.getX(0);
                float movey = event.getY(1) - event.getY(0);
                float moveLen = (float) Math.sqrt(moveX * moveX + movey * movey);
                float scale = (float) Math.log10(moveLen / downLen + 9);
                mMatrixScale.postScale(scale, scale);
                check();
                setImageMatrix(mMatrixScale);
                break;
            case MotionEvent.ACTION_POINTER_UP:
        }
    }

    private void check() {
        float[] p = new float[9];
        mMatrixScale.getValues(p);
        if (p[0] < 0.3f) {
            mMatrixScale.setScale(0.3f, 0.3f);
        }
        if (p[0] > 3f) {
            mMatrixScale.setScale(3f, 3f);
        }
    }


}
